package com.engine.downcustomfile.uitil;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author Administrator
 */
public class WorkflowInfoUtil {



    public static String getOneValue(String sql, Object... params) {
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql, params);
        return rs.next() ? rs.getStringNoTrim(1) : null;
    }

    public static String getWrokflowId(String requestId) {
        String workflowId = getOneValue("SELECT workflowid FROM workflow_requestbase WHERE requestid = ?", requestId);
        return workflowId;
    }

    public static String getTableName(String workflowId) {
        String tableName = getOneValue("SELECT tablename FROM workflow_bill left join workflow_base on workflow_bill.id = workflow_base.formid  WHERE workflow_base.id = ?", workflowId);
        return tableName;
    }

    public static String getCategory(String workflowId) {
        String docCategory_def = getOneValue("select docCategory from workflow_base where id=?", workflowId);

        if (!"".equals(docCategory_def) && docCategory_def.lastIndexOf(",") > -1) {
            docCategory_def = docCategory_def.substring(docCategory_def.lastIndexOf(",") + 1, docCategory_def.length());
        }
        return docCategory_def;
    }

    public static List<Map<String, String>> getData(RecordSet rs, String sql, Object... params) {
        List<Map<String, String>> datalist = new ArrayList<>();
        rs.executeQuery(sql, params);
        String[] queryFields = rs.getColumnName();

        while (rs.next()) {
            Map<String, String> rowmap = new TreeMap<>();

            for (String field : queryFields) {
                rowmap.put(field.toLowerCase(), rs.getStringNoTrim(field));
            }

            datalist.add(rowmap);
        }

        return datalist;
    }


}
